package com.ruoyi.framework.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.ruoyi.framework.interceptor.DatabaseNamePrefixInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

@Configuration
public class MyBatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(DataSource dataSource) {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        try (Connection connection = dataSource.getConnection()) {
            DatabaseNamePrefixInterceptor prefixInterceptor = new DatabaseNamePrefixInterceptor(connection);
            interceptor.addInnerInterceptor(prefixInterceptor);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return interceptor;
    }


}